var ApiFactory = require('lib/ApiFactory')
  , format = require('lib/util/format')
  , _ = require('underscore')
var gridSortMap = {
  combination1:['501', '502'], // 501 = 错误发生次数降序->设备影响数降序->最后发生时间降序; 502 = 设备影响数降序->错误发生次数降序->最后发生时间降序
  'timestamp':['102','101'],
  'executeTime':['104','103'],
  'totalTime':['106','105'],
  'firstTime':['302','301'],
  'lastTime':['506','505'],
  'count':['304','303'],
  'affected':['306','305'],
  'appVersion':['504','503']
}
function gridSortScendFn(order){
  var finded = _.chain(gridSortMap).pairs().map(function(pair){
    return {
      key:pair[0],
      indOf:pair[1].indexOf(order)
    };
  }).find(function(p){
    return p.indOf > -1
  }).value()
  if(finded){
    return {
      key:finded.key,
      scend:finded.indOf===0?1:-1
    }
  }else{
    throw new Error('');
  }
}
module.exports.gridSortScendFn = gridSortScendFn;
module.exports.createGridOrder = function(initKey,initScend){
  var key = initKey
    , scend = initScend
  var getOrder = function getOrder(){
    return {
      order:gridSortMap[key][scend > 0 ? 0 : 1]
    }
  }
  getOrder.setNewOrder = function(k,s){
    key = k;
    scend = s;
  }
  return getOrder
}
var tracelistApi = ApiFactory({
  url:function(params){
    return BlueWare.urlPrefix+'/app/'+params.appId+'/'+params.urlName+'.json'
  },
  optionParams:['urlName','appId'],
  params:{
    appId:{
      defaultValue:BlueWare.appId
    },
    urlName:{},
    order:{},
    spanTime:{
      defaultValue:30*60*1000
    },
    offset:{
      defaultValue:0
    },
    limit:{
      defaultValue:10
    },
    appVersionId:{
      defaultValue:BlueWare.appVersionId
    }
  }
})
module.exports.tracelistApi = tracelistApi
module.exports.parseToPartGrid = function parseToPartGrid(data,params,callback){
  var sortOption = gridSortScendFn(params.order)
  return {
    data:data,
    sortKey:sortOption.key,
    sortScend:sortOption.scend,
    hasMore:data.length === params.limit,
    showMore:function(){
      return tracelistApi(params,{
        offset:this.data.length
      }).fetch().then(function(innerData, innerParams) {
        if (callback) {
          callback(innerData);
        }
        return {
          data:innerData,
          hasMore:innerData.length === innerParams.limit
        }
      })
    }
  }
}

module.exports.parseToGrid = function parseToGrid(data, params, callback) {
  return {
    data:data,
    hasMore:data.length === params.limit,
    showMore:function(){
      return tracelistApi(params, {
        offset:this.data.length
      }).fetch().then(function(innerData, innerParams){
        if (callback) {
          callback(innerData.ajaxTraceListResults);
        }
        return {
          data:innerData.ajaxTraceListResults,
          hasMore:innerData.ajaxTraceListResults.length === innerParams.limit
        }
      })
    }
  };
}

module.exports.parseToGrid2 = function parseToGrid2(data, params, callback) {
  return {
    data:data,
    hasMore:data.length === params.limit,
    showMore:function(){
      return tracelistApi(params, {
        offset:this.data.length
      }).fetch().then(function(innerData, innerParams){
        if (callback) {
          callback(innerData)
        }
        return {
          data:innerData,
          hasMore:innerData.length === innerParams.limit
        }
      })
    }
  };
};
